package com.ethink.platform.controller.order;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.web.BaseController;
import com.ethink.framework.common.web.context.SPRequestContextHolder;
import com.ethink.framework.common.web.response.CommonResponse;
import com.ethink.order.api.TobSaleOrderFacade;
import com.ethink.order.api.TobSaleSettlementOrderFacade;
import com.ethink.order.api.enums.TobSaleSettlementOrderOrderStatusEnum;
import com.ethink.order.api.info.TobSaleSettlementOrderInfo;
import com.ethink.order.api.request.TobSaleSettlementOrderAddRequest;
import com.ethink.order.api.request.TobSaleSettlementOrderUpdateRequest;
import com.ethink.order.api.request.TobSaleSettlementPageQueryRequest;
import com.ethink.order.api.response.TobSaleSettlementQueryDetailResponse;
import com.ethink.platform.controller.order.info.TobSaleSettleImportInfo;
import com.ethink.platform.manager.SaleOrderManager;
import com.ethink.platform.manager.TobSaleSettleManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author default
 * @version 1.0.0
 * @ClassName TobSaleSettleController.java
 * @Description
 * @createTime 2022年11月17日 15:14:00
 */
@Tag(name = "TobSaleSettleController", description = "Tob结算 Controller")
@RestController
@Slf4j
@RequestMapping("platform-server/v1/order-service")
public class TobSaleSettleController extends BaseController {

    @Autowired
    private TobSaleSettlementOrderFacade tobSaleSettlementOrderFacade;

    @Autowired
    private TobSaleSettleManager tobSaleSettleManager;

    @Autowired
    private TobSaleOrderFacade tobSaleOrderFacade;

    @Autowired
    private SaleOrderManager saleOrderManager;


    @Operation(summary = "新增")
    @PostMapping("/tobSettle/add")
    public CommonResponse addTobSaleSettleOrder(@Validated @RequestBody TobSaleSettlementOrderAddRequest request) {
        request.setCreateUserName(SPRequestContextHolder.getCurrentSystemUserRealName());
        request.setCreateUserCode(SPRequestContextHolder.getCurrentSystemUserLoginName());
        request.setOrderStatus(TobSaleSettlementOrderOrderStatusEnum.S1.getIntValue());
        tobSaleSettlementOrderFacade.addTobSaleSettleOrder(request);
        return succeed();
    }

    @Operation(summary = "导入")
    @PostMapping("/tobSettle/import/{saleSubChannel}")
    public CommonResponse<List<TobSaleSettleImportInfo>> importTobSaleSettleOrder(@RequestParam("file") MultipartFile file
                                                                , @PathVariable String saleSubChannel) {
        return succeedWith(tobSaleSettleManager.importTobSaleSettleOrder(file,saleSubChannel));
    }


    @Operation(summary = "修改")
    @PutMapping("/tobSettle/update")
    public CommonResponse updateTobSaleSettleOrder(@RequestBody TobSaleSettlementOrderUpdateRequest updateRequest) {
        //目前仅支持修改订单来源
        tobSaleSettlementOrderFacade.update(updateRequest);
        return succeed();
    }

    @Operation(summary = "删除")
    @DeleteMapping("/tobSettle/delete/{id}")
    public CommonResponse deleteTobSaleSettleOrder(@PathVariable Long id) {
        tobSaleSettlementOrderFacade.delete(id);
        return succeed();
    }


    @Operation(summary = "分页查询")
    @GetMapping("/tobSettle/pageQuery")
    public CommonResponse<List<TobSaleSettlementOrderInfo>> pageQueryTobSaleSettleOrder(TobSaleSettlementPageQueryRequest request) {
        Page<TobSaleSettlementOrderInfo> page = tobSaleSettlementOrderFacade.pageQuery(request);
        return succeedWithPage(page);
    }



    @Operation(summary = "详情")
    @GetMapping("/tobSettle/detail/{id}")
    public CommonResponse<TobSaleSettlementQueryDetailResponse> queryDetailTobSaleSettleOrder(@PathVariable("id") Long id) {
        TobSaleSettlementQueryDetailResponse response = tobSaleSettlementOrderFacade.queryDetail(id);
        return succeedWith(response);
    }

    @Operation(summary = "匹配确认")
    @PutMapping("/tobSettle/match/{id}")
    public CommonResponse TobSettleMatchConfirm(@PathVariable("id") Long id){
        //结算预售单
        tobSaleOrderFacade.settleOrder(id);
        return succeed();
    }

}
